package com.googlecode.maratische.google;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.googlecode.maratische.google.model.Credentials;
import com.googlecode.maratische.google.model.Property;

public class LoginForm extends JDialog {

	private static final long serialVersionUID = -4376761815976655724L;
	private JTextField loginField;
	private JPasswordField passwordField;
	private JLabel infoLabel;
	private JButton buttonOk = new JButton("Enter");
	private JButton buttonCancel = new JButton("Cancel");
	private JButton offlineButton = new JButton("Offline");
	JCheckBox checkBox = new JCheckBox("Сохранять сессию (пароль не сохраняется!)");
	
	public LoginForm(MainFrame frame, String errorMessage) {
		super(frame,true);
		
		setTitle("Login to Google Reader");
		setSize(400, 300);
		setLayout(new GridLayout(5, 1));
		
		JPanel panel0 = new JPanel();
		infoLabel = new JLabel();
		panel0.add(infoLabel);
		add(panel0);
		
		JPanel panel1 = new JPanel();
		loginField = new JTextField(20);
		panel1.add(new JLabel("Login: "));
		panel1.add(loginField);
		add(panel1);
		
		JPanel panel2 = new JPanel();
		passwordField = new JPasswordField(20);
		panel2.add(new JLabel("Password: "));
		panel2.add(passwordField);
		add(panel2);
		
		JPanel panel3 = new JPanel();
		panel3.add(checkBox);
		add(panel3);
		
		JPanel panel4 = new JPanel();
		panel4.add(buttonOk);
		panel4.add(buttonCancel);
		panel4.add(offlineButton);
		add(panel4);
		
		offlineButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					MainController.getMainController().changeOfflineStatus(true);
				} catch (BaseException e1) {
					MainController.getMainController().showError(e1);
				}
				close();
			}
		});
		
		buttonOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		loginField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		passwordField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		
		//show login
		try {
//			String login = MainController.getMainController().getProperties().getProperty("login");
			Property propertyLogin = DaoAction.getDaoAction().getPropertyDao().getPropertyByType(Property.PROPERTY_LOGIN);
			String login = null;
			if (propertyLogin != null) {
				login = propertyLogin.getString1();
			}
			if (login != null && login.length()>0) {
				this.loginField.setText(login);
				SwingUtilities.invokeLater(new Runnable() { public void run() { passwordField.requestFocus(); } } );
			} else {
				SwingUtilities.invokeLater(new Runnable() { public void run() { loginField.requestFocus(); } } );
			}
		} catch (BaseException e1) {
			JOptionPane.showMessageDialog(this, e1.getMessage());
		}
		
		if (errorMessage != null) {
			infoLabel.setText(errorMessage);
		}
	}
	
	Credentials credentials = null;
		
	private void login() {
		credentials = new Credentials();
		credentials.setUserName(loginField.getText());
		credentials.setPassword(new String(passwordField.getPassword()));
		credentials.setSaveSession(checkBox.isSelected());
		close();
	}
	
	public Credentials getCredentials() {
		return credentials;
	}
	
	private void close() {
		this.dispose();
	}
	

}
